package com.lovo.order.mq;

import com.lovo.common.result.GlobalHandleException;
import com.lovo.common.util.MQUtil;
import com.lovo.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "seckill.queue")
public class SeckillReceiver {
    @Autowired
    private MQUtil mqUtil;
    @Autowired
    private OrderService orderService;
    @RabbitHandler
    public void receiver(String content, Message message, Channel channel){
        try{
            String [] contents = content.split(",");
            Integer userId = Integer.parseInt(contents[0]);
            Integer productId = Integer.parseInt(contents[1]);
            System.out.println("处理消息：" + userId + " " + productId);
            orderService.addSeckillOrder(message.getMessageProperties().getMessageId(),userId,productId);
            mqUtil.ack(message,channel);
        }catch (Exception | GlobalHandleException e){
            mqUtil.nack(message,channel);
        }


    }
}
